<script setup lang="ts">
import { useCodeLogin } from '@/composable'
import { loginByThird, bindMobile } from '@/services/login'
import { useUserStore } from '@/stores'
import type { User } from '@/types/user'
import { codeRules, MobileRules } from '@/utils/rules'
import { showFailToast, showSuccessToast } from 'vant'
import { onMounted, ref } from 'vue'
import { useRouter } from 'vue-router'

const router = useRouter()
const openId = ref('')
// 是否绑定手机号
const bindingFlag = ref(false)
const mobile = ref()
const code = ref('')
const checked = ref(false)
const user = useUserStore()
onMounted(() => {
  // 查看QQ登录是否成功，获取openId
  if (window.QC.Login.check()) {
    // 登录成功，调用第三方登录接口进行登录
    window.QC.Login.getMe((id) => {
      openId.value = id
      loginByThird(id)
        .then((data) => {
          // 登录成功之后需要记录下当前用户的个人信息
          // 如果已经绑定了手机号码，登录成功之后需要跳转到原地址或个人中心页
          loginSuccess(data)
        })
        .catch(() => {
          // 判断是否绑定手机号
          bindingFlag.value = true
        })
    })
  }
})
// 登录成功之后进行数据的存储
const loginSuccess = (res: { data: User }) => {
  user.updateUserInfo(res.data)
  router.replace(user.returnURL || '/user')
  showSuccessToast('登录成功！')
  user.updateReturnURL('')
}
// 绑定手机号
const checkForm = async () => {
  // 先判断是否有勾选已同意协议
  if (checked.value) {
    // 绑定手机号
    const data = await bindMobile({
      mobile: mobile.value,
      code: code.value,
      openId: openId.value
    })
    // 需要在登录QQ时记录当前路径上的returnURL属性，这里登录成功之后将跳转到该页面，如果没有则跳转到用户个人中心
    loginSuccess(data)
  } else {
    showFailToast('请勾选已同意协议')
  }
}
const { time, sendCode, form } = useCodeLogin(mobile, 'bindMobile')
</script>

<template>
  <div class="login-callback" v-if="bindingFlag">
    <DP-NavBar
      title="绑定手机号"
      @clickRight="$router.push('/register')"
    ></DP-NavBar>
    <van-form class="dp-form" @submit="checkForm" ref="form">
      <van-field
        v-model.trim="mobile"
        name="mobile"
        placeholder="请输入手机号"
        type="tel"
        :rules="MobileRules"
        autocomplete="off"
      ></van-field>
      <van-field
        v-model="code"
        placeholder="请输入验证码"
        :rules="codeRules"
        autocomplete="off"
      >
        <template #button>
          <span
            :class="['sendCode', time > 0 ? 'active' : '']"
            @click="sendCode"
            >{{ time <= 0 ? '发送验证码' : `${time}s后重新发送` }}</span
          >
        </template>
      </van-field>
      <div class="dp-cell">
        <van-checkbox v-model="checked">
          <span>我已同意</span>
          <a href="javascript:;" style="color: var(--cp-primary); margin: 0 3px"
            >用户协议</a
          >
          <span>及</span>
          <a href="javascript:;" style="color: var(--cp-primary); margin: 0 3px"
            >隐私条款</a
          >
        </van-checkbox>
      </div>
      <div class="dp-cell">
        <van-button type="primary" block round native-type="submit"
          >立即绑定</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<style lang="scss" scoped>
@import '@/styles/login.scss';
.login-callback {
  margin-top: 46px;
  .dp-form {
    padding: 50px 0;
  }
}
</style>
